package main

import (
    "os"
    "fmt"
    "errors"
    "reflect"
    "runtime"
    "kumachan/standalone/qt"
    "kumachan/standalone/util"
    "kumachan/standalone/util/argv"
    "kumachan/standalone/util/fatal"
    "kumachan/interpreter"
    "kumachan/interpreter/lang/textual/syntax"
    "kumachan/interpreter/runtime/def"
    "kumachan/interpreter/runtime/vm"
    "kumachan/interpreter/runtime/lib/env"
    "kumachan/support/atom"
)


const Version = "KumaChan 0.0.0 pre-alpha"
const NoProgramFilePrompt = "** No Program File or Directory Specified **\n" +
    "input a path or press enter to start REPL:"

type Args struct {
    Positional   [] string  `arg:"positional" hint:"[PATH [ARGUMENT]...]"`
    Command      string     `arg:"command" key:"help; version; atom; parse; run" default:"run" desc:"show this help; show version; run Atom Editor plugin backend service; parse the file at PATH; run the file or directory at PATH"`
    EnableRepl   bool       `arg:"flag-enable" key:"repl" desc:"enable REPL"`
    DebugUI      bool       `arg:"flag-enable" key:"debug-ui" desc:"enable ui debugging"`
    AsmDumpPath  string     `arg:"value-string" key:"asm-dump" hint:"FILE" desc:"dump ASM text to FILE"`
}

func main() {
    // Qt Main should be executed on main thread to avoid unknown problems
    runtime.LockOSThread()
    // get command line options
    var args Args
    var help, err = argv.ParseArgs(os.Args, reflect.ValueOf(&args))
    if err != nil {
        fatal.BadArgs(err, help)
    }
    switch args.Command {
    case "help":
        fmt.Fprintf(os.Stderr, "%s\n", help)
    case "version":
        fmt.Fprintf(os.Stderr, "%s\n", Version)
    case "atom":
        if len(args.Positional) == 0 {
            var err = atom.LangServer(os.Stdin, os.Stdout, os.Stderr)
            if err != nil { fatal.CrashGoError(err, "Crashed") }
        } else {
            // TODO: validate quantity of positional arguments in argv.go
            const problem = "redundant ARGUMENT(s) in 'atom' command"
            fatal.BadArgs(errors.New(problem), help)
        }
    case "parse":
        const default_root = syntax.DefaultRootPartName
        const repl_root = syntax.ReplRootPartName
        var L = len(args.Positional)
        if L == 0 {
            interpreter.DebugParser(os.Stdin, "(stdin)", repl_root)
        } else if L == 1 {
            var file = args.Positional[0]
            if file == "-" {
                interpreter.DebugParser(os.Stdin, "(stdin)", default_root)
            } else {
                f, err := os.Open(file)
                if err != nil { fatal.CrashGoError(err, "Crashed") }
                interpreter.DebugParser(f, f.Name(), default_root)
                err = f.Close()
                if err != nil { fatal.CrashGoError(err, "Crashed") }
            }
        } else {
            const problem = "redundant ARGUMENT(s) in 'parse' command"
            fatal.BadArgs(errors.New(problem), help)
        }
    case "run":
        var entry_path, no_file_specified, program_args =
            (func() (string, bool, ([] string)) {
                var L = len(args.Positional)
                if L == 0 {
                    return "", true, nil
                } else {
                    var entry_path = args.Positional[0]
                    var program_args = args.Positional[1:]
                    return entry_path, false, program_args
                }
            })()
        if no_file_specified {
            fmt.Fprintf(os.Stderr, "%s\n", NoProgramFilePrompt)
            var line, err = util.WellBehavedReadLine(os.Stdin)
            if err != nil { panic(err) }
            if len(line) > 0 {
                entry_path = string(line)
                no_file_specified = false
            }
        }
        var repl_config *interpreter.ReplConfig
        if args.EnableRepl || no_file_specified {
            repl_config = &interpreter.ReplConfig {
                Input:  os.Stdin,
                Output: os.Stderr,
            }
        }
        var options = &vm.Options {
            Environment:      env.NewDefaultEnvironment(program_args),
            ExecutionOptions: vm.ExecutionOptions {},
            DebuggingOptions: vm.DebuggingOptions {
                BasicOptions: def.DebugOptions {
                    DebugUI: args.DebugUI,
                },
                AsmDumpPath:  args.AsmDumpPath,
            },
        }
        if options.DebuggingOptions.BasicOptions.DebugUI {
            qt.EnableDebug()
        }
        go (func() {
            interpreter.Run(entry_path, options, repl_config)
            qt.NotifyNotUsed()
        })()
        qt.Main()
    default:
        panic("impossible branch")
    }
}


